package zero.tech.redis.config;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "redission")
@ConditionalOnProperty(prefix ="redission",name="enable",havingValue = "true",matchIfMissing = false)
public class RedissonConfig {
	
	private String mode;
	private String host;
	private Integer port;
	private String pwd;
	private String database;
	private Boolean enable;
	
	private String sentinelmastername;
	private String sentinelnodes;
	private String sentinelmasterconnectionpoolsize;
	private String sentinelslaveconnectionpoolsize;
	
	private String clusternodes;
	private String clustermasterconnectionpoolsize;
	private String clusterslaveconnectionpoolsize;
	
	
	
	@Bean
    public RedissonClient redisson() throws IOException {
		Config config = new Config();
        config.setTransportMode(TransportMode.NIO);
        config.setCodec(JsonJacksonCodec.INSTANCE);
        if(RedissionMode.STADALONE.equals(mode)||mode==null) {
        	config
        	.useSingleServer()
        	.setAddress("redis://" + host + ":" + (port==null?6379:port))
        	.setPassword(StringUtils.isBlank(pwd) ? null : pwd)
        	.setDatabase(StringUtils.isBlank(database)?1:Integer.valueOf(database))
        	;
        }else if(RedissionMode.SENTINEL.equals(mode)) {
        	 String[] sentinels=sentinelnodes.split(",");
             String[] addresses=new String[sentinels.length];
             for(int i=0;i<sentinels.length;i++) {
            	 addresses[i]="redis://"+sentinels[i];
             }
            config
            .useSentinelServers()
        	.setMasterName(sentinelmastername)
        	.addSentinelAddress(addresses)
        	.setPassword(StringUtils.isBlank(pwd)?null:pwd)
        	.setDatabase(StringUtils.isBlank(database)?1:Integer.valueOf(database))
        	.setMasterConnectionPoolSize(StringUtils.isBlank(sentinelmasterconnectionpoolsize)?64:Integer.valueOf(sentinelmasterconnectionpoolsize))
            .setSlaveConnectionPoolSize(StringUtils.isBlank(sentinelslaveconnectionpoolsize)?64:Integer.valueOf(sentinelslaveconnectionpoolsize))
        	;
        }else if(RedissionMode.CLUSTER.equals(mode)) {
        	 String[] clusters=clusternodes.split(",");
             String[] addresses=new String[clusters.length];
             for(int i=0;i<clusters.length;i++) {
            	 addresses[i]="redis://"+clusters[i];
             }
            config
            .useClusterServers()
            .setScanInterval(2000) 
            .addNodeAddress(addresses)
            .setPassword(StringUtils.isBlank(pwd)?null:pwd)
            .setMasterConnectionPoolSize(StringUtils.isBlank(clustermasterconnectionpoolsize)?64:Integer.valueOf(clustermasterconnectionpoolsize))
            .setSlaveConnectionPoolSize(StringUtils.isBlank(clusterslaveconnectionpoolsize)?64:Integer.valueOf(clusterslaveconnectionpoolsize))
            ;
        }
        return Redisson.create(config);
    }
	
	public String getMode() {
		return mode;
	}

	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public Integer getPort() {
		return port;
	}

	public void setPort(Integer port) {
		this.port = port;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getDatabase() {
		return database;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public String getSentinelmastername() {
		return sentinelmastername;
	}

	public void setSentinelmastername(String sentinelmastername) {
		this.sentinelmastername = sentinelmastername;
	}

	public String getSentinelnodes() {
		return sentinelnodes;
	}

	public void setSentinelnodes(String sentinelnodes) {
		this.sentinelnodes = sentinelnodes;
	}

	public String getSentinelmasterconnectionpoolsize() {
		return sentinelmasterconnectionpoolsize;
	}

	public void setSentinelmasterconnectionpoolsize(String sentinelmasterconnectionpoolsize) {
		this.sentinelmasterconnectionpoolsize = sentinelmasterconnectionpoolsize;
	}

	public String getSentinelslaveconnectionpoolsize() {
		return sentinelslaveconnectionpoolsize;
	}

	public void setSentinelslaveconnectionpoolsize(String sentinelslaveconnectionpoolsize) {
		this.sentinelslaveconnectionpoolsize = sentinelslaveconnectionpoolsize;
	}

	public String getClusternodes() {
		return clusternodes;
	}

	public void setClusternodes(String clusternodes) {
		this.clusternodes = clusternodes;
	}

	public String getClustermasterconnectionpoolsize() {
		return clustermasterconnectionpoolsize;
	}

	public void setClustermasterconnectionpoolsize(String clustermasterconnectionpoolsize) {
		this.clustermasterconnectionpoolsize = clustermasterconnectionpoolsize;
	}

	public String getClusterslaveconnectionpoolsize() {
		return clusterslaveconnectionpoolsize;
	}

	public void setClusterslaveconnectionpoolsize(String clusterslaveconnectionpoolsize) {
		this.clusterslaveconnectionpoolsize = clusterslaveconnectionpoolsize;
	}

	public Boolean getEnable() {
		return enable;
	}

	public void setEnable(Boolean enable) {
		this.enable = enable;
	}

	protected class RedissionMode{
		public static final String CLUSTER = "cluster";
		public static final String SENTINEL = "sentinel";
		public static final String STADALONE = "standalone";
		
	}
}
